package com.anjoyo.xgh.musicshare.application;

import java.io.File;
import java.util.ArrayList;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.app.Application;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.provider.MediaStore.Audio.Media;

import com.anjoyo.xgh.musicshare.activity.HomeActivity;
import com.anjoyo.xgh.musicshare.db.MusicDBhelper;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.Constant.PLAYLIST;
import com.anjoyo.xgh.musicshare.util.Constant.PLAYSTATUS;
import com.anjoyo.xgh.musicshare.util.JsonUtil;

public class SongApplication extends Application {
	static SongApplication instance;
	// 网络音乐列表
	public static ArrayList<Song> songslist = new ArrayList<Song>();
	// 本地音乐列表
	public static ArrayList<Song> localsongslist = new ArrayList<Song>();
	// 收藏音乐列表
	public static ArrayList<Song> favsongslist = new ArrayList<Song>();
	// 正在播放列表
	public static ArrayList<Song> currentlist = new ArrayList<Song>();
	public static MediaPlayer player = new MediaPlayer();
	PLAYLIST nowpalylist;
	public static int currentpos = -1;
	public static PLAYSTATUS status = PLAYSTATUS.WAITE;
	public static Song currentsong;
	public static MusicDBhelper dBhelper;
	public static boolean ispaused =false;
	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		System.out.println("application-oncreate啦");
		LoadNetSongs(20, "", null);
		LoadLocalSongs();
		favsongslist = LoadFavSongs();
	}

	public void LoadNetSongs(int num, String language, final Handler handler) {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("size", num + "");
		params.put("language", language);
		finalHttp.get(Constant.SEARCH_SONG, params, new AjaxCallBack<Object>() {
			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				ArrayList<Song> songs = JsonUtil.ParseNetSong(t.toString());
				if (songs != null) {
					songslist.clear();
					songslist.addAll(songs);
					currentlist.clear();
					currentlist.addAll(songs);
					nowpalylist = PLAYLIST.NET_SONGS_LIST;
					currentpos = -1;
					System.out.println("切换列表");
					if (handler != null) {
						handler.sendEmptyMessage(Constant.NETSONG_DOWLOAD_OK);
					}
				}
			}
		});
	}

	public void LoadLocalSongs() {
		Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
				null, null, null, null);
		Song song = null;
		localsongslist.clear();
		while (cursor.moveToNext()) {
			song = new Song();
			song.setMusicid(cursor.getInt(cursor.getColumnIndex(Media._ID)));
			song.setName(cursor.getString(cursor.getColumnIndex(Media.TITLE)));
			song.setAblum(cursor.getString(cursor.getColumnIndex(Media.ALBUM)));
			song.setArtist(cursor.getString(cursor.getColumnIndex(Media.ARTIST)));
			song.setLocal_url(cursor.getString(cursor
					.getColumnIndex(Media.DATA)));
			song.setIsdoffline(true);
			int duartion = cursor.getInt(cursor.getColumnIndex(Media.DURATION));
			if (duartion > 90000) {
				localsongslist.add(song);
			}
		}
		cursor.close();
	}

	public ArrayList<Song> LoadFavSongs() {
		favsongslist.clear();
		favsongslist.addAll(getDBhelper().getFavMusic());
		return favsongslist;
	}
	public ArrayList<Song> ishaveFavFile() {
		return ishavefile(favsongslist);
	}

	public static SongApplication getInstance() {
		return instance;
	}

	// ????
	public MediaPlayer getPlayer() {
		if (player == null) {
			player = new MediaPlayer();
		}
		return player;
	}

	public MusicDBhelper getDBhelper() {
		if (dBhelper == null) {
			dBhelper = new MusicDBhelper(instance);
		}
		return dBhelper;
	}

	public void changNowPlayList(PLAYLIST playlist) {
//		if (nowpalylist == playlist) {
//			return;
//		}
		currentlist.clear();
		if (playlist == PLAYLIST.LOCAL_MUSIC_LIST) {
			currentlist.addAll(localsongslist);
			nowpalylist = PLAYLIST.LOCAL_MUSIC_LIST;
		} else if (playlist == PLAYLIST.NET_SONGS_LIST) {
			currentlist.addAll(songslist);
			nowpalylist = PLAYLIST.NET_SONGS_LIST;
		} else if (playlist == PLAYLIST.FAV_MUSIC_LIST) {
			currentlist.addAll(favsongslist);
			nowpalylist = PLAYLIST.FAV_MUSIC_LIST;
		}
		currentpos = -1;
	}

	// 判断离线文件是否存在
	public ArrayList<Song> ishavefile(ArrayList<Song> songList) {
		for (int i = 0; i < songList.size(); i++) {
			if (songList.get(i).isIsnetMusic()) {
				File file = new File(Constant.MUSIC_PATH
						+ songList.get(i).getName() + ".mp3");
				if (file.exists() && file.length() > 10) {
					SongApplication.getInstance().getDBhelper()
							.updateoffline(songList.get(i).getMusicid(), true);
					SongApplication
							.getInstance()
							.getDBhelper()
							.updatelocalurl(songList.get(i).getMusicid(),
									file.getAbsolutePath());
					songList.get(i).setIsdoffline(true);
				} else {
					SongApplication.getInstance().getDBhelper()
							.updateoffline(songList.get(i).getMusicid(), false);
					songList.get(i).setIsdoffline(false);
				}
			}
		}
		return songList;
	}
}
